package com.zoo.diannaopeijian.controller;

import com.github.pagehelper.PageInfo;
import com.zoo.diannaopeijian.domain.Warehouse;
import com.zoo.diannaopeijian.service.WarehouseService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by zx583 on 2018-01-08.
 */
@Controller
@RequestMapping(value = "/warehouse*")
public class WarehouseController extends BaseController {
    @Resource
    private WarehouseService warehouseService;

    @GetMapping(value = "warehouseManaPage")
    public String warehouseManaPageRoute() {
        return "warehouse/warehouseManaPage";
    }

    @GetMapping(value = "warehouseAddPage")
    public String warehouseAddPageRoute() {
        return "warehouse/warehouseAddPage";
    }

    @GetMapping(value = "warehouseUpdatePage")
    public String warehouseUpdatePageRoute() {
        return "warehouse/warehouseUpdatePage";
    }

    @PostMapping(value = "warehouseGet")
    @ResponseBody
    public Map<String, Object> warehouseGet(@RequestParam(required = true, defaultValue = "1") Integer page,
                                            @RequestParam(required = true, defaultValue = "10") Integer rows,
                                            @RequestParam(required = true, defaultValue = "false") boolean isFuzzy,
                                            Warehouse warehouse
    ) {
        Map<String, Object> result = new HashMap<>();

        List<Warehouse> datas = isFuzzy ? warehouseService.getFuzzyWarehouseWithPage(page, rows, warehouse) : warehouseService.getWarehouseWithPage(page, rows, warehouse);

        result.put(rowsWord, datas);
        result.put(totalWord, new PageInfo<Warehouse>(datas).getTotal());

        return result;
    }

    @PostMapping(value = "warehouseList")
    @ResponseBody
    public List<Warehouse> warehouseList() {
        return warehouseService.getWarehouse(null);
    }

    @PostMapping(value = "warehousePost")
    public @ResponseBody
    String warehousePost(Warehouse warehouse) {
        return warehouseService.addWarehouse(warehouse) ? success : fail;
    }

    @PostMapping(value = "warehouseDelete")
    @ResponseBody
    public String warehouseDelete(Integer warehouseid) {
        return warehouseService.deleteWarehouse(warehouseid) ? success : fail;
    }

    @PostMapping(value = "warehouseUpdate")
    @ResponseBody
    public String warehouseUpdate(Warehouse warehouse) {
        return warehouseService.updateWarehouse(warehouse) ? success : fail;
    }
}
